/*9IvejywmIOnnbJQ0iXrTRsOEtFbnvO6SaSObh8pZEiS36eipr212tmmAr0Regexv*/
// 用于生成凭证、联查凭证
import { cardCache,cacheTools,ajax,viewModel } from 'nc-lightapp-front';
const { setDefData } = cardCache;
let { setGlobalStorage} = viewModel;
import { 
    dataSource,dataSourceDetail, 
    voucher_link,voucher_gen,
    listCacheKey,cardCacheKey,linkToCacheTool
} from '../components/constJSON';

// param:
//{
//     voucher:voucher, //完整的凭证vo  [vo1,vo2]/vo
//     pk_voucher: '...',//联查时，凭证id [pk1,pk2]/pk
//     titlename:'凭证生成',
//     backUrl:'/',
//     backAppCode:backAppCode,
//     backPageCode:backPageCode
// }

export function openToVoucher(_this,param){
    if (Array.isArray(param.pk_voucher)&&param.pk_voucher.length>1) {
        let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"Y"});
        let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
        let listUrl = '/gl/gl_voucher/pages/main/index.html#/'; //凭证维护-列表
        if(isNew){
            listUrl=pageUrl;
        }
        cacheTools.set(linkToCacheTool, { id: param.pk_voucher });
        _this.props.openTo(listUrl, {
            status: 'browse',
            appcode: appcode,
            c: appcode,
            //pagekey: 'link',
            scene : '_LinkVouchar',
            n: param.titlename,/* 国际化处理： 凭证联查*/
            ifshowQuery: true,
            pagecode:pagecode
        });
    } else {
        let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"N"});
        let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
        let pk_voucher=param.pk_voucher;
        let cardUrl = '/gl/gl_voucher/pages/main/index.html#/Welcome'; //凭证维护-卡片
        let cardPageUrl = appcode === '20020PREPA' ? cardUrl : `${cardUrl}/?type=query`; //凭证维护-卡片:凭证查询-卡片
        if(isNew){
            cardPageUrl=pageUrl;
        }
        if(Array.isArray(param.pk_voucher)){
            pk_voucher=param.pk_voucher[0]
        }
        _this.props.openTo(cardPageUrl,
            {
                appcode: appcode,
                c: appcode,
                id: pk_voucher,
                //pagekey: 'link',
                scene : '_LinkVouchar',
                n: param.titlename,/* 国际化处理： 凭证联查*/
                status: 'browse',
                backflag: 'noback',
                pagecode:pagecode
            }
        )
    }

}
function checkLinkVoucherRouter(voucherapp){
    let appcode = voucherapp.appcode;
    let pagecode=voucherapp.pagecode;
    let pageUrl=voucherapp.pageurl;
    let isNew=false;
    let pageListRoute='/list';
    let pageCardRoute='/welcome';
    if(pageUrl.indexOf('gl/voucher/pages')!=-1){
        isNew=true;
        pageListRoute='/voucherList';
        pageCardRoute='/voucherCard';
    }
    return{appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute};
}
export function pushToGenVoucher(_this,param){
    if (param.voucher.gl_voucher||param.voucher.voucher.gl_voucher||param.voucher.voucher.list) {
        let voucherapp = voucherRelatedApp({sence:voucher_gen,isList:"Y",isEdit:'Y'});
        let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
        //多张凭证到列表 
        setDefData(listCacheKey, dataSource, param.voucher.voucher);
        _this.props.pushTo(pageListRoute, {//"/gl/voucher/pages/main/index.html#/list"
            appcode: appcode,  
            pagecode:pagecode,
            ifshowQuery: true, //是否显示查询区，true不显示，false显示，默认显示
            n: param.titlename,/* 国际化处理： 凭证生成*/
            //pagekey: 'generate',
            backflag: 'back',
            backUrl: param.backUrl?param.backUrl:'/',
            backAppCode:param.backAppCode,
            backPageCode:param.backPageCode,
        },{
            'paramInUrl':isNew?true:false
        });
    } else {
        let voucherapp = voucherRelatedApp({sence:voucher_gen,isList:"N",isEdit:'Y'});
        let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
        //单张凭证到卡片
        setDefData(cardCacheKey, dataSourceDetail, param.voucher);
        _this.props.pushTo(pageCardRoute, {//voucherapp.pageurl
            status: 'edit',
            appcode: appcode,
            pagecode:pagecode,
            //pagekey: 'generate',
            ifshowQuery: true, 
            n: param.titlename,/* 国际化处理： 凭证生成*/
            backflag: 'back',
            backUrl: param.backUrl?param.backUrl:'/',
            backAppCode: param.backAppCode,
            backPageCode: param.backPageCode
        },{
            'paramInUrl':isNew?true:false
        });
    }
}

export function pushToLinkVoucher(_this, param) {
    let urlParam = {
        ifshowQuery: true,
        n: param.titlename,
        //pagekey: 'link',
        scene : '_LinkVouchar',
        status: 'browse',
        // backUrl: param.backUrl?param.backUrl:'/',
        backAppCode: param.backAppCode,
        backPageCode: param.backPageCode,
    }
    if(param.voucher){
        //预览(未保存的凭证联查)
        if(Array.isArray(param.voucher)&&param.voucher.length>1){
            let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"Y"});
            let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
            let listUrl = '/gl/gl_voucher/pages/main/index.html#/'; //凭证维护-列表
            if(isNew){
                listUrl=pageUrl;
            }
            urlParam.appcode=appcode;
            urlParam.pagecode=pagecode;
            setGlobalStorage(listCacheKey, dataSource, JSON.stringify(param.voucher));
            urlParam.backflag='noback';
            // _this.props.pushTo(pageListRoute, urlParam);
            _this.props.openTo(listUrl, urlParam);
        }else{
            let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"N"});
            let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
            let cardUrl = '/gl/gl_voucher/pages/main/index.html#/Welcome'; //凭证维护-卡片
            let cardPageUrl = appcode === '20020PREPA' ? cardUrl : `${cardUrl}/?type=query`; //凭证维护-卡片:凭证查询-卡片
            if(isNew){
                cardPageUrl=pageUrl;
            }
            urlParam.appcode=appcode;
            urlParam.pagecode=pagecode;
            setGlobalStorage(cardCacheKey, dataSourceDetail, JSON.stringify(param.voucher));
            urlParam.backflag='noback';
            // _this.props.pushTo(pageCardRoute, urlParam);
            _this.props.openTo(cardPageUrl,urlParam);
        }
    }else{
        //联查
        setDefData(listCacheKey, dataSource, param.pk_voucher);
        if(Array.isArray(param.pk_voucher)&&param.pk_voucher.length>1){
            let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"Y"});
            let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
            let listUrl = '/gl/gl_voucher/pages/main/index.html#/'; //凭证维护-列表
            if(isNew){
                listUrl=pageUrl;
            }
            urlParam.appcode=appcode;
            urlParam.pagecode=pagecode;
            setGlobalStorage(listCacheKey, dataSource, JSON.stringify(param.pk_voucher));
            urlParam.backflag = 'noback';
            // _this.props.pushTo(pageListRoute, urlParam);
            _this.props.openTo(listUrl, urlParam);
        }else{
            let voucherapp = voucherRelatedApp({sence:voucher_link,isList:"N"});
            let {appcode,pagecode,pageUrl,isNew,pageListRoute,pageCardRoute}=checkLinkVoucherRouter(voucherapp);
            let cardUrl = '/gl/gl_voucher/pages/main/index.html#/Welcome'; //凭证维护-卡片
            let cardPageUrl = appcode === '20020PREPA' ? cardUrl : `${cardUrl}/?type=query`; //凭证维护-卡片:凭证查询-卡片
            if(isNew){
                cardPageUrl=pageUrl;
            }
            urlParam.appcode=appcode;
            urlParam.pagecode=pagecode;
            let pk_voucher = param.pk_voucher;
            if (Array.isArray(param.pk_voucher)) {
                pk_voucher = param.pk_voucher[0]
            }
            setGlobalStorage(cardCacheKey, dataSourceDetail, JSON.stringify(param.pk_voucher));
            urlParam.backflag = 'noback';
            urlParam.id = pk_voucher;
            // _this.props.pushTo(pageCardRoute, urlParam);
            _this.props.openTo(cardPageUrl,urlParam);
        }
    }


}

export function voucherRelatedApp(params) {
    let {sence,isList,isEdit}=params;
    let url = '/nccloud/gl/voucher/voucherRelatedApp.do';
    let data = {
        sence: sence,
        billtype: 'C0',
        isList:isList,
        isEdit:isEdit
    }
    let appcode = '';
    ajax({
        url,
        data: data,
        async: false,
        success: function (response) {
            if (response.success) {
                appcode = response.data
            }
        }
    });
    return appcode
}
/*9IvejywmIOnnbJQ0iXrTRsOEtFbnvO6SaSObh8pZEiS36eipr212tmmAr0Regexv*/